'use strict';
const db = uniCloud.database();

exports.main = async (event, context) => {
    const { username, password, role } = event;
    const numPassword = Number(password);
    const res = await db.collection('users').where({
        username: username
    }).get();
    if (res.data.length > 0) {
        const result = {
            code: 401,
            msg: '用户名已存在'
        };
        console.log('[云函数：addUser]返回结果：', result);
        return result;
    } else {
        await db.collection('users').add({
            username: username,
            password: numPassword,
            role: role
        });
        const result = {
            code: 200,
            msg: '用户添加成功'
        };
        console.log('[云函数：addUser]返回结果：', result);
        return result;
    }
};